library(stargazer)
library(foreign)
setwd("~/Replication Code/")

##---Loading in data ----
ccap0812 <- read.dta("./data/ccap0812_subset.dta")

cces1014 <- read.dta("./data/cces1014_subset.dta")

gss0610 <- read.dta("./data/gss0610_subset.dta")

gss0812 <- read.dta("./data/gss0812_subset.dta")

gss1014 <- read.dta("./data/gss1014_subset.dta")

#----------------------------------------------------------------------------------------------------#
# CCAP 2008-2012 ----
#----------------------------------------------------------------------------------------------------#
dat1 <- na.omit(ccap0812[c("weight", "rr_12_sc", "rr_m_08_sc",
                            "pid7r_12_sc", "pid7r_m_08_sc")])

# PID on RR
m1 <- lm(rr_12_sc ~ pid7r_m_08_sc + rr_m_08_sc, dat1, weights = weight)
summary(m1)

# RR on PID
m10 <- lm(pid7r_12_sc ~ pid7r_m_08_sc + rr_m_08_sc, 
          dat1, weights = weight)
summary(m10)

#----------------------------------------------------------------------------------------------------#
# CCES 2010-2014 ----
#----------------------------------------------------------------------------------------------------#
dat2 <- na.omit(cces1014[c("weight", "rr_10_sc", "rr_14_sc", 
                            "pid7_10_sc", "pid7_14_sc")])

# PID on RR
m2 <- lm(rr_14_sc ~ pid7_10_sc + rr_10_sc, 
         dat2, weights = weight)
summary(m2)

# RR on PID
m20 <- lm(pid7_14_sc ~ pid7_10_sc + rr_10_sc, 
          dat2, weights = weight)
summary(m20)

#----------------------------------------------------------------------------------------------#
### Table 1----
m1_p <- m1
m10_p <- m10
m2_p <- m2
m20_p <- m20

names(m1_p$coefficients) <- names(coef(m1_p))
names(m10_p$coefficients) <- names(coef(m1_p))
names(m2_p$coefficients) <- names(coef(m1_p))
names(m20_p$coefficients) <- names(coef(m1_p))


stargazer(m1_p, m10_p, m2_p, m20_p, 
          title = "Relationship between Whites' Partisanship and Racial Resentment, CCAP and CCES Panels",
          covariate.labels = c("Partisanship$_{t-1}$",
                               "Racial Resentment$_{t-1}$"),
          dep.var.labels = c("Racial Resentment$_t$", "Partisanship$_t$",
                             "Racial Resentment$_t$", "Partisanship$_t$"), 
          column.labels = c("2008-2012 CCAP", "2010-2014 CCES"),
          column.separate = c(2,2),
          no.space = T, 
          notes = c("$^{*}$p$<$0.05. OLS regression results with standard errors in parentheses. Variables scaled 0-1. Analyses use population weights."), 
          notes.align = "l", intercept.bottom = T, notes.append = F,
          dep.var.caption = "", model.numbers = F,
          table.placement = "t",
          digits = 3, df = F, omit.stat = c("f", "adj.rsq"),
          align = T, star.char = c("*"), star.cutoffs = c(0.05), header = F
)

#----------------------------------------------------------------------------------------------#
### GSS----

#----------------------------------------------------------------------------------------------#
# 2006-2010 -----
#----------------------------------------------------------------------------------------------#
dat3 <- na.omit(gss0610[c("rrKC_3_sc", "rrKC_1_sc", "pid_1_sc", "pid_3_sc", "wtpannr123")])

#### PID on RR
m1_0610 <- lm(rrKC_3_sc ~  pid_1_sc  + rrKC_1_sc, 
              data = dat3, weights = wtpannr123)
summary(m1_0610)


#### RR on PID
m10_0610 <- lm(pid_3_sc ~  pid_1_sc  + rrKC_1_sc, 
               data = dat3, weights = wtpannr123)
summary(m10_0610)

#----------------------------------------------------------------------------------------------#
# 2008-2012 -----
#----------------------------------------------------------------------------------------------#
dat4 <- na.omit(gss0812[c("rrKC_3_sc", "rrKC_1_sc", "pid_1_sc", "pid_3_sc", "wtpannr123")])

#### PID on RR
m1_0812 <- lm(rrKC_3_sc ~  pid_1_sc  + rrKC_1_sc, 
              data = dat4, weights = wtpannr123)
summary(m1_0812)

#### RR on PID
m10_0812 <- lm(pid_3_sc ~  pid_1_sc  + rrKC_1_sc, 
               data = dat4, weights = wtpannr123)
summary(m10_0812)

#----------------------------------------------------------------------------------------------#
# 2010-2014 -----
#----------------------------------------------------------------------------------------------#
dat5 <- na.omit(gss1014[c("rrKC_3_sc", "rrKC_1_sc", "pid_1_sc", "pid_3_sc", "WTPANNR123")])

#### PID on RR
m1_1014 <- lm(rrKC_3_sc ~  pid_1_sc  + rrKC_1_sc, 
              data = dat5, weights = WTPANNR123)
summary(m1_1014)

#### RR on PID
m10_1014 <- lm(pid_3_sc ~  pid_1_sc  + rrKC_1_sc, 
               data = dat5, weights = WTPANNR123)
summary(m10_1014)


#----------------------------------------------------------------------------------------------#
# Table 2 -----
# Formatting for table
m1_0610_p <- m1_0610
m10_0610_p <- m10_0610
m1_0812_p <- m1_0812
m10_0812_p <- m10_0812
m1_1014_p <- m1_1014
m10_1014_p <- m10_1014
 

stargazer(m1_0610_p, m10_0610_p, m1_0812_p, m10_0812_p, m1_1014_p, m10_1014_p,
          title = "Relationship between Whites' Partisanship and Racial Resentment, GSS Panels",
          covariate.labels = c("Partisanship$_{t-1}$",
                               "Racial Resentment$_{t-1}$"),
          dep.var.labels = c("Racial Resentment$_t$", "Partisanship$_t$",
                             "Racial Resentment$_t$", "Partisanship$_t$",
                             "Racial Resentment$_t$", "Partisanship$_t$"), 
          column.labels = c("2006-2010", "2008-2012", "2010-2014"),
          column.separate = c(2,2,2),
          no.space = T, 
          notes = c("$^{*}$p$<$0.05. OLS regression results with standard errors in parentheses. Variables scaled 0-1. Analyses use population weights."), 
          notes.align = "l", intercept.bottom = T, notes.append = F,
          dep.var.caption = "", model.numbers = F, table.placement = "t",
          digits = 3, df = F, omit.stat = c("f", "adj.rsq"),
          align = T, star.char = c("*"), star.cutoffs = c(0.05), header = F
)
